* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file runs additional regressions for domestic market outcomes at the product level.

********************************************************************************
*Prepare data for target in "wide" format to be merged with A-data
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\vars_subset.dta", clear 
drop if cvrnr==""
gen vare=substr(vare_nr,1,8)
drop if vare=="00000000"

rename cvrnr cvrnrA
rename aar aarA
rename vare vareA

destring vareA, gen(cn8A)
rename vaerdi vrdA
collapse (sum) vrdA, by(cvrnrA aarA cn8A)

rename vrdA vrdA_vars
merge 1:1 cvrnrA aarA cn8A using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_product.dta", keep(match master) keepusing(vrdA) nogen
replace vrdA = 0 if vrdA==.
replace vrdA_vars = vrdA_vars*1000
gen vrdA_dom = vrdA_vars - vrdA
drop if vrdA_dom<=0

rename cvrnr cvrnrT
rename aar year
rename cn8A cn8
rename vrdA_dom vrdT
collapse (sum) vrdT, by(cvrnrT year cn8)
merge m:1 cn8 year using "D:\Data\Workdata\703989\fjsd3989\concordance\cn8_cn8plus_1993_2018.dta", keep(match) nogen

rename year aarT
collapse (sum) vrdT, by(cvrnrT aarT cn8plus)
merge m:1 cvrnrT using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", keep(match) keepusing(cvrnrA aar) nogen

*Lump all targets together
rename cvrnrT cvrnrT_ori
gen dup = 1
merge m:1 cvrnrA aar dup using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", keep(match) 

keep if (aarT>=aar-3) & (aarT<=aar+3)
gen d_yr = aarT-aar
gen after = (d_yr>0)
keep if after==0
* Products in all years before merger (including year of merger)
collapse (sum) vrdT, by (cvrnrA cvrnrT aar aarT cn8plus)
rename aarT aarA
tempfile tempT_long_before
save `tempT_long_before.dta', replace

********************************************************************************

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", clear
keep cvrnrA aar
duplicates drop
sort cvrnrA aar
* Number of years
by cvrnrA: gen dup2= _n
tempfile temp_did_step2_dup2
save `temp_did_step2_dup2.dta', replace
collapse (max) dup2, by(cvrnrA)
rename dup maxdup2
tempfile temp_did_step2_maxdup2
save `temp_did_step2_maxdup2.dta', replace

********************************************************************************

*Prepare data for acquirer in "wide" format to be merged with T-data
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\vars_subset.dta", clear 
drop if cvrnr==""
gen vare=substr(vare_nr,1,8)
drop if vare=="00000000"

rename cvrnr cvrnrA
rename aar aarA
rename vare vareA

destring vareA, gen(cn8A)
rename vaerdi vrdA
collapse (sum) vrdA, by(cvrnrA aarA cn8A)

rename vrdA vrdA_vars
merge 1:1 cvrnrA aarA cn8A using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_product.dta", keep(match master) keepusing(vrdA) nogen
replace vrdA = 0 if vrdA==.
replace vrdA_vars = vrdA_vars*1000
gen vrdA_dom = vrdA_vars - vrdA
drop if vrdA_dom<=0

rename aar year
rename cn8A cn8
drop vrdA
rename vrdA_dom vrdA
collapse (sum) vrdA, by(cvrnrA year cn8)
merge m:1 cn8 year using "D:\Data\Workdata\703989\fjsd3989\concordance\cn8_cn8plus_1993_2018.dta", keep(match) nogen

rename year aarA
collapse (sum) vrdA, by(cvrnrA aarA cn8plus)
rename cn8plus cn8plusA

merge m:1 cvrnrA using `temp_did_step2_maxdup2.dta', keep(match) nogen
expand maxdup2
bysort cvrnrA aarA cn8plusA: gen dup2 = _n
merge m:1 cvrnrA dup2 using `temp_did_step2_dup2.dta', keep(match) nogen
drop *dup*

gen d_yr = aarA-aar

drop if aar < 1996 | aar > 2015

gen dup = 1
merge m:1 cvrnrA aar dup using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", keep(match) keepusing(cvrnrT) nogen

keep if (aarA>=aar-3) & (aarA<=aar+3)
gen after = (d_yr>0)

rename cn8plusA cn8plus
preserve
keep if after==0
* Products in all years before merger (including year of merger)
keep cvrnrA cn8plus aar aarA cvrnrT vrdA
tempfile tempA_long_before
save `tempA_long_before.dta', replace

restore
keep if after==1
* Products in all years after merger
keep cvrnrA cn8plus aar aarA cvrnrT vrdA
tempfile tempA_long_after
save `tempA_long_after.dta', replace

use `tempA_long_before.dta', clear
append using `tempA_long_after.dta'
merge 1:1 cvrnrA cvrnrT aar cn8plus aarA using `tempT_long_before.dta', nogen
rename vrdA vrdA_yearly
rename vrdT vrdT_yearly
tempfile tempAproduct
save `tempAproduct.dta', replace

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_product_cn8plus.dta", replace
keep if vrdA_before!=. | vrdT_before!=. | vrdA_after!=.
bysort cvrnrA aar: gen nprod_ba = _N
keep cvrnrA aar cn8plus vrdA_before vrdA_after vrdT_before nprod_ba
sort cvrnrA aar cn8plus
by cvrnrA aar: gen productno = _n
tempfile tempnprod_1
save `tempnprod_1.dta', replace
collapse (mean) nprod_ba, by(cvrnrA aar)
tempfile tempnprod
save `tempnprod.dta', replace

********************************************************************************
* Main data
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_domestic.dta", clear

drop minemployee_cvrnr
* Calculate minimum number of employees in sample
egen minemployee_cvrnr = min(employee_cvrnr) if nprodAT>0 & nprodAT!=., by(cvrnrA)

gen p = (p1==1 | p2==1 | p3==1)

keep if minemployee_cvrnr>=10 & minemployee_cvrnr!=.
merge m:1 cvrnrA aar using `tempnprod.dta', keep(match) nogen
expand nprod_ba
bysort cvrnrA aar aarA: gen productno = _n
merge m:1 cvrnrA aar productno using `tempnprod_1.dta', keep(match) nogen
merge 1:1 cvrnrA aar aarA cn8plus using `tempAproduct.dta', keep(match master) nogen
foreach x in vrdA_yearly vrdT_yearly vrdA_before vrdA_after vrdT_before{
	replace `x' = 0 if `x'==.
	}

gen pvrdA_yearly = (vrdA_yearly>0 | vrdT_yearly>0)

*Overlapping
gen Oproduct = (vrdA_before>0 & vrdT_before>0)
*Non-overlapping
gen NOproduct = ((vrdA_before>0 & vrdT_before==0) | (vrdA_before==0 & vrdT_before>0))
*New
gen NWproduct = (vrdA_before==0 & vrdT_before==0)
*A only
gen Aproduct = (vrdA_before>0 & vrdT_before==0)
*T only
gen Tproduct = (vrdA_before==0 & vrdT_before>0)

foreach x in O NO NW A T {
	egen m`x'product = max(`x'product), by(cvrnrA aar cn8plus)
	gen pXm`x'product = p*m`x'product
	}

*Mutually exclusive and exhaustive
gen allcat = mOproduct + mNOproduct + mNWproduct
sum allcat

gen allcat2 = mOproduct + mAproduct + mTproduct + mNWproduct
sum allcat2

* Table A.14
areg pvrdA_yearly p i.aarA i.group_cvrnr if minemployee_cvrnr>=10 & (acquirer==1 & ((nprodT>0 & d_yr<=0) | d_yr>0)), absorb(cn8plus) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A14.txt", keep(p) bdec(4) sdec(4) nocons nonotes 

areg pvrdA_yearly p mOproduct pXmOproduct mNOproduct pXmNOproduct i.aarA i.group_cvrnr if minemployee_cvrnr>=10 & (acquirer==1 & ((nprodT>0 & d_yr<=0) | d_yr>0)), absorb(cn8plus) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A14.txt", keep(p mOproduct pXmOproduct mNOproduct pXmNOproduct) bdec(4) sdec(4) nocons nonotes 

areg pvrdA_yearly p i.aarA i.group_cvrnr if minemployee_cvrnr>=10 & (acquirer==1 & ((nprodT>0 & d_yr<=0) | d_yr>0)) & mNWproduct==0, absorb(cn8plus) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A14.txt", keep(p) bdec(4) sdec(4) nocons nonotes

areg pvrdA_yearly p mNOproduct pXmNOproduct i.aarA i.group_cvrnr if minemployee_cvrnr>=10 & (acquirer==1 & ((nprodT>0 & d_yr<=0) | d_yr>0)) & mNWproduct==0, absorb(cn8plus) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A14.txt", keep(p mNOproduct pXmNOproduct) bdec(4) sdec(4) nocons nonotes 

areg pvrdA_yearly p mAproduct pXmAproduct mTproduct pXmTproduct i.aarA i.group_cvrnr if minemployee_cvrnr>=10 & (acquirer==1 & ((nprodT>0 & d_yr<=0) | d_yr>0)) & mNWproduct==0, absorb(cn8plus) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A14.txt", keep(p mAproduct pXmAproduct mTproduct pXmTproduct) bdec(4) sdec(4) nocons nonotes 